package com.wwm.springcloud.authservice.config;

import com.wwm.springcloud.authservice.exception.BwExceptionHandlerExceptionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuwanming
 */
@EnableWebMvc
@Configuration
public class WebMVCConfiguration extends WebMvcConfigurationSupport
{
    /**
     * 统一异常处理
     * @return
     */
    @Bean
    @Override
    public ExceptionHandlerExceptionResolver handlerExceptionResolver()
    {
        BwExceptionHandlerExceptionResolver exceptionResolver = new BwExceptionHandlerExceptionResolver();
        exceptionResolver.setOrder(0);
        exceptionResolver.setMessageConverters(messageConverters());
        return exceptionResolver;
    }

    private MappingJackson2HttpMessageConverter jsonHttpMessageConverter()
    {
        return new MappingJackson2HttpMessageConverter();
    }

    private List<HttpMessageConverter<?>> messageConverters()
    {
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(jsonHttpMessageConverter());
        return messageConverters;
    }
}